﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShowComet.aspx.cs" Inherits="Derrick.Web.ShowComet" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

        <script type="text/javascript" src="/Scripts/jquery-1.7.1.js" ></script>

        <script language="javascript" >

            function getXmlHttpRequestObject() {
                if (window.XMLHttpRequest) {
                    return new XMLHttpRequest();
                }
                else if (window.ActiveXObject) {
                    return new ActiveXObject("Microsoft.XMLHTTP");
                }
                else {
                    document.getElementById("p_status").innerHTML("Status: Could not create XmlHttpRequest Object." + "Consider upgrading your browser.");
                }
            }

            var sendReq = getXmlHttpRequestObject();
            var receiveReq = getXmlHttpRequestObject();
            var lastMessage;
            var mTimer;

            //Gets the server response:
            function getResponse() {
                if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
                    //receiveReq.open("POST", 'http://tardis:1802', true, "server", "server123");
                    //receiveReq.open("POST", 'http://localhost:1802', true, "server", "server123");
                    //receiveReq.open("POST", 'http://holit109:1802', true, "server", "server123");
                    //receiveReq.open("POST", "http://localhost:1802", true, "server", "server123");
                    receiveReq.open("POST", "http://L45723:1802", true, "server", "server123");  //must use this URL at work.
                    receiveReq.onreadystatechange = handleReceiveMessage;
                    receiveReq.setRequestHeader("Content-Type", "application/xml");
                    //receiveReq.timeout = 200;
                    receiveReq.send("<?xml version='1.0'?><Request><Command>Queue</Command><User>user1</User><Message>Message 1</Message><Message>Message 2</Message><Message>Message 3</Message></Request>");
                }
            }

            //function for handling the return message from Comet
            function handleReceiveMessage() {
                if (receiveReq.readyState == 4) {
                    //var tmpDoc = document;
                    //var msgDiv = document.getElementById("Request");
                    //alert("Message received!");
                    document.getElementById("_receivedMsgLabel").innerHTML += "Message received!<br/>";
                    var status = receiveReq.status;
                    document.getElementById("_receivedMsgLabel").innerHTML += "Status received!<br/>";
                    var txt = receiveReq.responseText;
                    //var txt = eval("(" + receiveReq.responseText + ")");

                    //if (txt == null) {
                    //    return;
                    //}

                    //var xmldoc = new ActiveXObject('Microsoft.XMLDOM');
                    //alert("Instantiated XML DOM object...");
                    //var doc = parser.parseFromString(text, 'text/xml');
                    //alert("Parsed from string");
                    //xmldoc.loadXML(txt);
                    //var xmldoctmp = $.parseXML(txt);
                    //alert("String has been parsed.");
                    //var xmldoc = $(xmldoctmp);
                    //alert("Loaded XML...");
                    //var xmldoc = $(txt);
                    //var messageNodes = xmldoc.getElementsByTagName("Message");
                    //alert("Searched for message nodes.");
                    //var numMessages = messageNodes.length;
                    //alert("Number of message nodes: " + numMessages.toString());

                    //for (i = 0; i < numMessages; i++) {
                    //    var msgNode = messageNodes[i].text;
                    //    alert("Adding to label: " + msgNode.toString());
                    //    document.getElementById("_receivedMsgLabel").innerHTML += msgNode.toString();
                    //    msgDiv += msgNode.toString();
                    //}
                    //mTimer = setTimeout("getResponse();", 2000);

                    document.getElementById("_receivedMsgLabel").innerHTML += txt + "<br/>";
                    //document.getElementById("_receivedMsgLabel").innerHTML += txt.Message + "<br/>";

                    //mTimer = setTimeout("getResponse();", 200);
                }

                getResponse();
            }

</script>

</head>
<body onload="getResponse()">
    <form id="form1" runat="server">
    <div>
 
     <asp:Label ID="_receivedMsgLabel" runat="server" ClientIDMode="Static"></asp:Label>
        
    </div>
    </form>
</body>
</html>
